import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

export default new Router({
    routes: [
        {
            path: '/',
            redirect: '/login'
        },
        {
            path: '/index',
            component: resolve => require(['../components/common/Home.vue'], resolve),
            beforeEnter: (to, from, next) => {
                if (!localStorage.getItem('token')) {
                    next({
                        path: '/login',
                        query: { redirect: to.fullPath }
                    })
                }else {
                    //Vue.$axios.defaults.headers.common['Authorization'] = localStorage.getItem('token');
                    next()
                }

            },
            children:[
                {
                    path:'/',
                    component:resolve => require(['../components/page/Dashboard.vue'], resolve)
                },
                {
                    path:'/users',
                    component: resolve => require(['../components/page/Users.vue'], resolve)
                },
                {
                    path:'/usersAccount',
                    component: resolve => require(['../components/page/UsersAccount.vue'], resolve)
                },
                {
                    path:'/accountCreate',
                    component: resolve => require(['../components/page/AccountCreate.vue'], resolve)
                },
                {
                    path:'/userCreate',
                    component: resolve => require(['../components/page/UserCreate.vue'], resolve)
                }
            ]
        },
        {
            path: '/login',
            component: resolve => require(['../components/page/Login.vue'], resolve)
        },
    ]
})
